
 /** 
 * Project Name:b2b-merBusi 
 * File Name:FileUtil.java 
 * Package Name:com.umpay.utils 
 * Date:2018年1月29日下午3:56:24 
 * Copyright (c) 2018, xmfsmasara@163.com.com All Rights Reserved. 
 * 
*/  
  
package com.umpay.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/** 
 * ClassName:FileUtil <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2018年1月29日 下午3:56:24 <br/> 
 * @author   wangpengbo 
 * @version   
 * @see       
 */
public class FileUtil {
	
	private static final Logger log = LogManager.getLogger(FileUtil.class);

	
	 /*****************************************************************
	 * checkFileExist:(校验文件是否存在). 
	 * @author wangpengbo 
	 * @param filePaths
	 * @return
	 *****************************************************************/ 
	 
	public static boolean checkFileExist(String... filePaths) {
		for (String filePath : filePaths) {
			// 文件判断
			File file = new File(filePath);
			if (!file.exists()) {
				log.error(String.format("文件【%s】不存在", filePath));
				return false;
			}
			log.info(String.format("文件【%s】已存在", filePath));
		}
		return true;
	}
		
	
	 /*****************************************************************
	 * copy:(文件复制). 
	 * @author wangpengbo 
	 * @param sourceFile
	 * @param targetFile
	 * @throws Exception
	 *****************************************************************/ 
	 
	public static void copy(File sourceFile, File targetFile) throws Exception {
		int byteread = 0;
		if (sourceFile.exists()) {
			InputStream ins = null;
			FileOutputStream outs = null;
			try {
				ins = new FileInputStream(sourceFile);
				outs = new FileOutputStream(targetFile);
				byte[] buffer = new byte[4096];
				while ((byteread = ins.read(buffer)) != -1)
					outs.write(buffer, 0, byteread);
			} finally {
				if (ins != null)
					ins.close();
				if (outs != null)
					outs.close();
			}
		} else {
			throw new Exception("源文件不存在[" + sourceFile.getAbsolutePath() + "]");
		}
	}

	
	/** 数据按行写入fileName中 
	 * @param <T>**/
	public static <T> void writeFileByLine(String fileName, List<T> datas,String fileHead) throws Exception{
		CreateFileUtil.createFile(fileName);
		FieldableFile fieldableFile = new FieldableFile(fileName);
		try {
			fieldableFile.open(true, ",","GBK");
			if(StringUtils.isNotEmpty(fileHead)){
				WriteBody(fieldableFile,fileHead);
			}
			log.info(String.format("开始写入对账文件[%s]!", fileName));
			for(T data:datas){
				WriteBody(fieldableFile,data);
			}
			log.info(String.format("写入对账文件[%s]成功!", fileName));
			fieldableFile.close();
		} catch (IOException e) {
			log.info(String.format("写入对账文件[%s]失败,失败原因为[%s]", fileName, e.getMessage()));
			throw new Exception(e);
		}finally{
			fieldableFile.close();
		}
	}
	
	public static void WriteHead(FieldableFile ff, String fileHead)
			throws Exception {
		try {
			ff.open(true, ",");
			ff.writeLine(fileHead);
		} catch (IOException e) {
			log.info("文件打开异常： ");
			ff.close();
			throw new Exception(e.getMessage());
		}
	}

	public static void WriteBody(FieldableFile ff, Object obj)
			throws Exception {
		try {
			ff.writeLine(obj.toString());
		} catch (IOException e) {
			log.info("文件写入异常： ");
			ff.close();
			throw new Exception(e.getMessage());
		}
	}

}
  